package org.liaohailong.helloworld.practicemiddle;

/**
 * Author: liaohailong
 * Time: 2021/7/5 15:34
 * Describe: 中级算法 - 链表
 */
public class LinkedListPractice {
    public static final class ListNode {
        int val;
        public ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode create(int[] arr) {
        ListNode head = null;
        ListNode node = null;
        for (int i : arr) {
            if (head == null) {
                head = new ListNode(i);
                node = head;
            } else {
                node.next = new ListNode(i);
                node = node.next;
            }
        }
        return head;
    }

    public ListNode tail(ListNode head) {
        ListNode node = head;
        while (node.next != null) {
            node = node.next;
        }
        return node;
    }

    public void print(ListNode head) {
        StringBuilder sb = new StringBuilder();
        ListNode node = head;
        sb.append("[");
        while (node != null) {
            sb.append(node.val);
            sb.append(",");
            node = node.next;
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append("]");
        System.out.println(sb.toString());
    }

    // LC 两数相加
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
//        输入：l1 = [2,4,3], l2 = [5,6,4]
//        输出：[7,0,8]
//        解释：342 + 465 = 807.

        ListNode head = new ListNode();
        ListNode node = head;
        int extra = 0;
        while (true) {
            int sum = -1;
            if (l1 != null && l2 != null) {
                sum = l1.val + l2.val;
                l1 = l1.next;
                l2 = l2.next;
            } else if (l1 != null) {
                sum = l1.val;
                l1 = l1.next;
            } else if (l2 != null) {
                sum = l2.val;
                l2 = l2.next;
            }
            if (sum == -1 && extra > 0) {
                sum = extra;
                extra = 0;
            }
            if (sum == -1) break;

            if (extra > 0) {
                sum += extra;
                extra = 0;
            }
            if (sum >= 10) {
                extra = sum / 10;
                sum %= 10;
            }
            node.next = new ListNode(sum);
            node = node.next;
        }
        return head.next;
    }

    // LC 奇偶链表
    public ListNode oddEvenList(ListNode head) {
        if (head == null || head.next == null) return head;

        ListNode node = head;

        ListNode oddHead = null;
        ListNode odd = null;
        ListNode evenHead = null;
        ListNode even = null;

        while (node != null) {
            if (odd == null) {
                odd = node;
                oddHead = odd;
            } else {
                odd.next = node;
                odd = odd.next;
            }
            node = node.next;

            if (node != null) {
                if (even == null) {
                    even = node;
                    evenHead = even;
                } else {
                    even.next = node;
                    even = even.next;
                }
                node = node.next;
            }
        }

        even.next = null;
        odd.next = evenHead;
        return oddHead;
    }

    // LC 相交链表
//    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
//        ListNode nodeA = headA;
//        ListNode nodeB = headB;
//
//        HashSet<ListNode> record = new HashSet<>();
//        while (nodeA != null) {
//            record.add(nodeA);
//            nodeA = nodeA.next;
//        }
//        while (nodeB != null) {
//            if (record.contains(nodeB)) return nodeB;
//            nodeB = nodeB.next;
//        }
//        return null;
//    }

    // LC 相交链表
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        //相交的 pa+pb = pb+pa
        if (headA == null || headB == null) {
            return null;
        }
        ListNode pa = headA;
        ListNode pb = headB;
        while (pa != pb) {
            pa = pa == null ? headB : pa.next;
            pb = pb == null ? headA : pb.next;
        }
        return pa;
    }
}
